package springjunit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

import org.apache.commons.collections.map.LinkedMap;

public class workAssign {

	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		List<User> users=new ArrayList<User>();
		User user=new User("wife up","1");
		User user2=new User("wu doctor","2");
		User user3=new User("user3 up","3");
		User user4=new User("user4","4");
		User user5=new User("users5 up","5");
		User user6=new User("users6","6");
		User user7=new User("users7","7");
		
		users.add(user);
		users.add(user2);
		users.add(user3);
		users.add(user4);
		users.add(user5);
		users.add(user6);
		//users.add(user7);
		  
		Collections.sort(users); 
	    for (User p : users) { 
	        System.out.println(p.getName()); 
	    }
		
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
       // System.out.println("当前日期：               "+sf.format(c.getTime()));
        c.add(Calendar.DAY_OF_MONTH, 19);//19需要调整
       // System.out.println("增加20天后日期 ：  "+sf.format(c.getTime()));
        
        
       /* Scanner sc = new Scanner(System.in); 
        System.out.println("输入排班开始日期，请安yyyy-mm-dd格式输入，如:2014-10-29");  
        String startMonth=sc.next();
        System.out.println("输入字符串："+startMonth);
        System.out.println("输入排班结束日期，请安yyyy-mm-dd格式输入，如:2014-10-29");  
        String endMonth=sc.next();
        //需要配置的天数
        long intervalDays=getDaySub(startMonth,endMonth);*/
        long intervalDays=getDaySub("2014-10-29","2015-02-01")+1;
        
        String worktime;
        int userIndex=1;
        String userName=new String();
       // Map<String,String> assignTable = Collections.synchronizedMap(new TreeMap<String,String>());
       Map<String,String> assignTable = Collections.synchronizedMap(new LinkedMap());
       
        //startMonth=
       // endMonth=
        int dayOfWeek;
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        for(int month=0;month<intervalDays;month++){
        	//System.out.println("*******users.size*********"+users.size()+"***"+userIndex);
        	if(userIndex%users.size()==0)
        	{
        		userName=users.get(userIndex-1).getName();
        		userIndex=1; 
        	}
        	else{ 
        		userName=users.get(userIndex-1).getName();
        	    userIndex++;
        	}
            c.add(Calendar.DAY_OF_MONTH, 1);
            
            
            worktime=sf.format(c.getTime());
            dayOfWeek=c.get(Calendar.DAY_OF_WEEK)-1;
            
    		assignTable.put(worktime+' '+weekDays[dayOfWeek],userName);
    		
        }
    		String value;
    		Calendar dayc1 = new GregorianCalendar();
    		Map<String,String> result = Collections.synchronizedMap(new LinkedMap());
    		 Iterator<String> iterator = assignTable.keySet().iterator();  
    		     while(iterator.hasNext()) {  
    		         String key = iterator.next();  
    		          value = assignTable.get(key);
    		         
    		    if(value.endsWith("up") && key.endsWith("星期六"))
    		    {
    		    	String oldkey=key;
    		    	String keytime=key.substring(0, key.indexOf(' '));
    		    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    		    	Date d = df.parse(keytime);    //start_date是类似"2013-02-02"的字符串
    		    	dayc1.setTime(d);
    		    	dayc1.add(Calendar.DAY_OF_MONTH, 1);
    		    	worktime=sf.format(dayc1.getTime());
    	            dayOfWeek=dayc1.get(Calendar.DAY_OF_WEEK)-1;
    	            
    	            userName=assignTable.get(worktime+' '+weekDays[dayOfWeek]);
    	            
    		    	String tempUserName=assignTable.get(key);
    		    	//交换
    		    	result.put(oldkey,userName);//加上交换后的值
    		    	result.put(worktime+' '+weekDays[dayOfWeek],tempUserName);
    		    	iterator.next();//周日的数据已被替换，因此该数据已处理过，无需再处理，直接跳过即可
    		    }
    		    else
    		    	result.put(key, value);
    		     }
    		
    	
    		for (String key : result.keySet()) {
    			 value = result.get(key);
    			 if(value.endsWith("wife up"))
    			 System.out.println("******user "+value+" is assigned at time "+key+"********");

    		}
        

	
	}
	
	 /*功能描述：时间相减得到天数
     */
    public static long getDaySub(String beginDateStr,String endDateStr)
    {
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");    
        java.util.Date beginDate;
        java.util.Date endDate;
        try
        {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);    
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);    
            //System.out.println("相隔的天数="+day);   
        } catch (ParseException e)
        {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }   
        return day;
    }


}
